// C11:GuardEvenGenerator.cpp {RunByHand}
//  Simplifying mutexes with the Guard template.
//  {L} ZThread
#include "EvenChecker.h"
#include "zthread/Guard.h"
#include "zthread/Mutex.h"
#include "zthread/ThreadedExecutor.h"
#include <iostream>

using namespace ZThread;
using namespace std;

class GuardEvenGenerator : public Generator {
  unsigned int currentEvenValue;
  Mutex lock;

public:
  GuardEvenGenerator() { currentEvenValue = 0; }
  ~GuardEvenGenerator() { cout << "~GuardEvenGenerator" << endl; }
  int nextValue() {
    Guard<Mutex> g(lock);
    ++currentEvenValue;
    Thread::yield();
    ++currentEvenValue;
    return currentEvenValue;
  }
};

int main(void) { EvenChecker::test<GuardEvenGenerator>(); }